package com.xtourms.client.rm.product;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.Context;
import com.xtourms.client.ui.util.LocationWidget;
import com.xtourms.client.ui.util.MoneyTextBox;
import com.xtourms.client.util.MysqlDateFormat;
import com.xtourms.shared.field.ProductField;
import com.xtourms.shared.field.WorkerField;

public class ProductDetailManager extends Composite {

	private static ProductDetailManagerUiBinder uiBinder = GWT
			.create(ProductDetailManagerUiBinder.class);

	interface ProductDetailManagerUiBinder extends
			UiBinder<Widget, ProductDetailManager> {
	}

	private ContentFrame parent;
	private JSONObject pdtObj;
	private AsyncCallback<String> callback;
	
	public ProductDetailManager(ContentFrame parent, AsyncCallback<String> asyncCallback) {
		initWidget(uiBinder.createAndBindUi(this));
		this.parent = parent;
		this.pdtObj = new JSONObject();
		this.callback = asyncCallback;
		initWidget();
	}
	
	public ProductDetailManager(JSONObject pdtObj, ContentFrame parent, AsyncCallback<String> asyncCallback) {
		initWidget(uiBinder.createAndBindUi(this));
		this.parent = parent;
		this.pdtObj = pdtObj;
		this.callback = asyncCallback;
		initWidget();
		initProduct();
	}

	@UiField TextBox pdt_name;
	@UiField LocationWidget location;
	@UiField TextArea pdt_des;
	@UiField MoneyTextBox pdt_bp;
	@UiField MoneyTextBox pdt_sp;
	
	@UiField TextBox manager_name;
	@UiField TextBox manager_mobile;
	@UiField TextBox linkman_name;
	@UiField TextBox linkman_mobile;
	@UiField TextBox linkman_qq;

	@UiField DateBox start_date;
	@UiField DateBox end_date;
	
	private void initWidget() {
		manager_mobile.setMaxLength(12);
		linkman_mobile.setMaxLength(12);
		linkman_qq.setMaxLength(30);
		start_date.setFormat(new MysqlDateFormat());
		end_date.setFormat(new MysqlDateFormat());
	}
	
	private void initProduct() {
		pdt_name.setValue(((JSONString)pdtObj.get(ProductField.PDT_NAME)).stringValue());
		String pN = ((JSONString)pdtObj.get(ProductField.PDT_PROVINCE_NAME)).stringValue();
		String cN = ((JSONString)pdtObj.get(ProductField.PDT_CITY_NAME)).stringValue();
		location.setLocation(pN, cN);
		pdt_des.setValue(((JSONString)pdtObj.get(ProductField.PDT_DES)).stringValue());
		pdt_bp.setMoney(((JSONNumber)pdtObj.get(ProductField.PDT_BP)).toString());
		pdt_sp.setMoney(((JSONNumber)pdtObj.get(ProductField.PDT_SP)).toString());
		start_date.setValue(java.sql.Date.valueOf(((JSONString)pdtObj.get(ProductField.PDT_SD)).stringValue()));
		end_date.setValue(java.sql.Date.valueOf(((JSONString)pdtObj.get(ProductField.PDT_ED)).stringValue()));
		
		manager_name.setValue(((JSONString)pdtObj.get(ProductField.PDT_MANAGER_NAME)).stringValue());
		manager_mobile.setValue(((JSONString)pdtObj.get(ProductField.PDT_MANAGER_PHONE)).stringValue());
		linkman_name.setValue(((JSONString)pdtObj.get(ProductField.PDT_LINKMAN_NAME)).stringValue());
		linkman_mobile.setValue(((JSONString)pdtObj.get(ProductField.PDT_LINKMAN_PHONE)).stringValue());
		linkman_qq.setValue(((JSONString)pdtObj.get(ProductField.PDT_LINKMAN_QQ)).stringValue());
	}
	
	@UiHandler("confirm")
	void handleConfirm(ClickEvent evt){
		String hName = pdt_name.getText();
		pdtObj.put(ProductField.PDT_NAME, new JSONString(hName));	
		int cId = location.getSelectedCityId();
		pdtObj.put(ProductField.PDT_CITY_ID, new JSONNumber(cId));
		String hDes = pdt_des.getText();
		pdtObj.put(ProductField.PDT_DES, new JSONString(hDes));
		int bp = pdt_bp.getMoney();
		pdtObj.put(ProductField.PDT_BP, new JSONNumber(bp));
		int sp = pdt_sp.getMoney();
		pdtObj.put(ProductField.PDT_SP, new JSONNumber(sp));
		Date sDate = start_date.getValue();
		pdtObj.put(ProductField.PDT_SD, new JSONString(sDate.toString()));
		Date eDate = end_date.getValue();
		pdtObj.put(ProductField.PDT_ED, new JSONString(eDate.toString()));
		
		String mName = manager_name.getText();
		pdtObj.put(ProductField.PDT_MANAGER_NAME, new JSONString(mName));
		String mMobile = manager_mobile.getText();
		pdtObj.put(ProductField.PDT_MANAGER_PHONE, new JSONString(mMobile));
		String lName = linkman_name.getText();
		pdtObj.put(ProductField.PDT_LINKMAN_NAME, new JSONString(lName));
		String lMobile = linkman_mobile.getText();
		pdtObj.put(ProductField.PDT_LINKMAN_PHONE, new JSONString(lMobile));
		String lQq = linkman_qq.getText();
		pdtObj.put(ProductField.PDT_LINKMAN_QQ, new JSONString(lQq));

		pdtObj.put(ProductField.LASTMODIFY_WORKER, Context.ins.worker.get(WorkerField.WORKER_ID));
		
		ProductManagerFrame.ps.cOrMProduct(pdtObj.toString(), callback);
	}
	
	@UiHandler("cancel")
	void handleCancel(ClickEvent evt){
		parent.resumeContent();
	}
}
